package yxy.game.pm2.command.therionHunting;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.rank.RankModule;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TherionHuntingMessage;
import yxy.game.pm2.module.therionHunting.TherionChallengingPosition;
import yxy.game.pm2.module.therionHunting.TherionHandle;
import yxy.game.pm2.module.therionHunting.TherionModule;

import java.util.Map;

/**
 * @author wrs
 * @name CMD_TherionHuntingBattle
 * @descrition 神兽猎场战斗界面
 * @since 2022-06-10
 */
@MessageType(proto = Command.TherionHuntingBattle, classtype = TherionHuntingMessage.TherionHuntingBattleRequest.class)
public class CMD_TherionHuntingBattle extends Command<TherionHuntingMessage.TherionHuntingBattleRequest> {

    @Override
    public void execute(MessageSource messageSource, TherionHuntingMessage.TherionHuntingBattleRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int gameType = request.getGameType();
        int therionId = request.getTherionId(); //神兽ID

        TherionHuntingMessage.TherionHuntingBattleResponse.Builder response = TherionHuntingMessage.TherionHuntingBattleResponse.newBuilder();

        TherionHandle handle = new TherionHandle();
        try {
            //神兽key
            int therionType = handle.getTherionKey(therionId);
            if (therionType == -1) {
                send(messageSource, response, Result.ParamError);
                return;
            }

            //具体玩法的某个神兽信息
            TherionModule therionModule = TherionModule.get(gameType, playerCode, therionType);
            if (therionModule == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //神兽的所有挑战位
            Map<Integer, TherionChallengingPosition> positionMap = TherionChallengingPosition.getAll(gameType, playerCode, therionType);
            if (positionMap == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //神兽战斗界面信息处理
            Result handleTherionModuleResult = handle.handleTherionModule(playerCode, gameType, therionType, therionModule, positionMap);
            if (!handleTherionModuleResult.equals(Result.Success)) {
                send(messageSource, response, handleTherionModuleResult);
                return;
            }

            RankModule rankModule = handle.getRankModule(gameType, playerCode, therionType);
            Result packTherionBattleUiResult = handle.packTherionBattleUiInfo(playerCode, therionModule, positionMap, rankModule, response);
            if (!packTherionBattleUiResult.equals(Result.Success)) {
                send(messageSource, response, packTherionBattleUiResult);
                return;
            }

            send(messageSource, response, Result.Success);

        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, TherionHuntingMessage.TherionHuntingBattleResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
